package com.klikmee.sms.chat.web.usecases;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import com.dotexe.webframework.core.ClientApplication;
import com.dotexe.webframework.core.clientactions.MessageBox;
import com.dotexe.webframework.core.datasources.CollectionDataSource;
import com.dotexe.webframework.core.datasources.ObjectDataSource;
import com.dotexe.webframework.core.triggers.Trigger;
import com.klikmee.sms.chat.web.dao.BulkGroupItemDAO;
import com.klikmee.sms.chat.web.dto.NumberPrefix;
import com.klikmee.sms.chat.web.util.SpringBeansLookup;
import com.klikmee.sms.services.chat.model.BulkGroupItem;

public class BulkGroupAddItemForm extends ClientApplication {

	private ObjectDataSource dsBulkGroupItem;
	private ArrayList<String> bcBulkGroupItem;
	private CollectionDataSource dsNumberPrefix;
	private ArrayList<String> bcNumberPrefix;

	private BulkGroupItem group;

	private List<NumberPrefix> listOfPrefix = new ArrayList<NumberPrefix>();

	private boolean itemAdded;

	// override
	@Override
	public void load() throws Exception {
		super.load();

		this.dsBulkGroupItem = new ObjectDataSource("dsBulkGroupItem");
		this.getDataSources().add(this.dsBulkGroupItem);

		this.dsNumberPrefix = new CollectionDataSource("dsNumberPrefix");
		this.getDataSources().add(this.dsNumberPrefix);
		this.bcNumberPrefix = new ArrayList<String>();
		this.bcNumberPrefix.add("Name");

		this.bcBulkGroupItem = new ArrayList<String>();
		this.bcBulkGroupItem.add("PhoneNumber");
		this.bcBulkGroupItem.add("Note");

		this.triggers.add(new Trigger(this, "btnSaveClick"));
		this.triggers.add(new Trigger(this, "btnCancelClick"));
	}

	@Override
	public void start() throws Exception {
		super.start();

		this.dsBulkGroupItem.setSource(group);
		this.dsBulkGroupItem.bind(bcBulkGroupItem);

		getNumberPrefix();
	}

	public void btnSaveClick() throws JSONException {
		//zbog trigera koji se opaljuje na parentu!
		setItemAdded(false);
		BulkGroupItemDAO bulkGroupDAO = SpringBeansLookup
				.getBean(BulkGroupItemDAO.class);

		if (this.dsNumberPrefix.getSelectedItem() == null) {
			MessageBox.Show(this, "Info", "Morate odabrati prefix!");
			return;
		}

		String number = ((BulkGroupItem) this.dsBulkGroupItem.getSource())
				.getPhoneNumber();
		
		if(number == null)
		{
			MessageBox.Show(this, "Info", "Morate uneti broj telefona!");
			return;
		}
		Pattern pattern = Pattern.compile("^[0-9]{6,7}$");
		Matcher matcher = pattern.matcher(number);

		if (!matcher.find()) {
			MessageBox.Show(this, "Info", "Telefonski broj mora imati 6 ili 7 cifara!");
			return;
		}

		String numberPrefix = ((NumberPrefix) this.dsNumberPrefix
				.getSelectedItem()).getName();

		group.setPhoneNumber(numberPrefix + number);
		group.setNote(((BulkGroupItem) this.dsBulkGroupItem.getSource())
				.getNote());

		bulkGroupDAO.create(group);

		setItemAdded(true);
	}

	public void btnCancelClick() throws JSONException {
		this.hide();
	}

	public void setGroup(BulkGroupItem group) {
		this.group = group;
	}

	public BulkGroupItem getGroup() {
		return group;
	}

	public void getNumberPrefix() throws SecurityException,
			ClassNotFoundException, NoSuchFieldException,
			IllegalAccessException, JSONException, NoSuchMethodException,
			InvocationTargetException {
		listOfPrefix.clear();
		listOfPrefix.add(new NumberPrefix("38160"));
		listOfPrefix.add(new NumberPrefix("38161"));
		listOfPrefix.add(new NumberPrefix("38162"));
		listOfPrefix.add(new NumberPrefix("38163"));
		listOfPrefix.add(new NumberPrefix("38164"));
		listOfPrefix.add(new NumberPrefix("38165"));
		listOfPrefix.add(new NumberPrefix("38169"));

		this.dsNumberPrefix.setSource(listOfPrefix);
		this.dsNumberPrefix.bind(bcNumberPrefix);
	}

	public void setItemAdded(boolean itemAdded) {
		this.itemAdded = itemAdded;
	}

	public boolean getItemAdded() {
		return itemAdded;
	}
}
